package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;
import com.hermit.qmst.tools.Preferences;
import com.hermit.qmst.tools.RequestHttp;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by hermithermit on 15/2/16.
 */
public class PayCardActivity extends Activity implements View.OnClickListener {

    private EditText metCardpwd;
    private EditText metCardno;
    private EditText metPhoneNum;
    private EditText et_code;
    private String mCardpwd;
    private String mCardno;
    private String mPhoneNum;
    private TextView tvPhone;
    private ProgressDialog mProgressDialog;
    private Preferences preferences;
    private String TAG = "PayCardActivity_";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_card);
        preferences = new Preferences(PayCardActivity.this);
        initView();
    }

    private void initView() {
        metCardpwd = (EditText) findViewById(R.id.et_cardpwd).findViewById(R.id.et_clear);
        metCardno = (EditText) findViewById(R.id.et_cardnum).findViewById(R.id.et_clear);
        metPhoneNum = (EditText) findViewById(R.id.et_phonenum).findViewById(R.id.et_clear);
        et_code = (EditText) findViewById(R.id.et_code).findViewById(R.id.et_clear);
        metCardno.setHint("充值卡号");
        et_code.setHint("行业代码");
        metCardpwd.setHint("充值密码");
        metPhoneNum.setText(Common.myPhone);
        metPhoneNum.setEnabled(false);
        findViewById(R.id.bt_onlinecharge).setOnClickListener(this);
        findViewById(R.id.bt_charge).setOnClickListener(this);
        ((TextView) findViewById(R.id.tv_title)).setText("充值手机号");
        findViewById(R.id.iv_back).setOnClickListener(this);

    }

    private void request_charge() {
        String url = Common.submitRechargPath() + "username=" + mPhoneNum + "&cardno=" + mCardno + "&addpwd=" + mCardpwd + "&cardtype=3&money=5&agent=" + Common.mAgentId + "&acct=" +
                et_code.getText().toString();
        Log.i(TAG + "_url", url, true);
        RequestHttp.getHttp(PayCardActivity.this, url, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                try {
                    Toast.makeText(PayCardActivity.this,jsonObject.getString("error"), Toast.LENGTH_LONG).show();
                    if (jsonObject.getBoolean("state")) {
                        Common.mAgentId = jsonObject.getString("agent_id");
                        Common.mAgentThirdId = jsonObject.getString("agent_id");
                        preferences.setPreferenceStringValue(Preferences.SIP_AGENT_THIRD, jsonObject.getString("agent_id_third"));
                        preferences.setPreferenceStringValue(Preferences.SIP_AGENT, jsonObject.getString("agent_id"));
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.bt_charge:
//                preferences.setPreferenceStringValue(Preferences.SIP_AGENT,"8");

                mPhoneNum = metPhoneNum.getText().toString().trim();
                if (mPhoneNum.length() == 0) {
                    Toast.makeText(PayCardActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }

                mCardno = metCardno.getText().toString().trim();
                if (mCardno.length() == 0) {
                    Toast.makeText(PayCardActivity.this, "请输入卡号", Toast.LENGTH_SHORT).show();
                    return;
                }
                mCardpwd = metCardpwd.getText().toString().trim();
                if (mCardpwd.length() == 0) {
                    Toast.makeText(PayCardActivity.this, "请输入卡密", Toast.LENGTH_SHORT).show();
                    return;
                }

                request_charge();

                break;
            case R.id.iv_back:
                finish();
                //overridePendingTransition(R.anim.);

                break;
            case R.id.bt_onlinecharge:

//                Intent intent = new Intent(PayCardActivity.this, PayListActivity.class);
//                startActivity(intent);

                break;

            default:
                break;
        }

    }
}
